package com.ntschy.web.controller.bt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ntschy.bt.entity.dataobject.BtHelp.BtHelp;
import com.ntschy.bt.entity.dto.BtHelpDTO.InputDTO.BtHelpInputDTO;
import com.ntschy.bt.entity.dto.BtHelpDTO.InputDTO.BtHelpInputPageDTO;
import com.ntschy.bt.entity.dto.BtHelpDTO.ReplyDTO.BtHelpReplyDTO;
import com.ntschy.bt.entity.dto.BtHelpDTO.ReplyDTO.BtHelpReplyPageDTO;
import com.ntschy.bt.entity.enums.BtHelpStatusEnum;
import com.ntschy.bt.entity.mapstruct.BtHelpInputMapstruct;
import com.ntschy.bt.entity.mapstruct.BtHelpReplyMapstruct;
import com.ntschy.bt.entity.vo.BtHelp.Input.BtHelpInputDetialsVO;
import com.ntschy.bt.entity.vo.BtHelp.Input.BtHelpInputPageVO;
import com.ntschy.bt.entity.vo.BtHelp.Reply.BtHelpReplyDetialsVO;
import com.ntschy.bt.entity.vo.BtHelp.Reply.BtHelpReplyPageVO;
import com.ntschy.bt.entity.vo.BtHelp.Review.BtHelpReviewPageVO;
import com.ntschy.bt.service.IBtHelp.IBtHelpInputService;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author
 * @since
 */

@Validated
@RestController
@RequestMapping("/bt/help")
public class BtHelpInputController extends BaseController {
    @Resource
    private IBtHelpInputService btHelpInputService;

    // * ================================================基础增删改查BEGIN============================================= *
    /**
     * 求助反馈分页列表
     *
     * @param btHelpInputPageDTO
     * @return
     */
    @ApiOperation(value = "查询结果录入列表", notes = "")
    @ApiImplicitParam(name = "pageWaitEntryHelp", value = "结果录入分页查询", required = true, dataType = "BtHelpReviewPageDTO")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:page')")
    @PostMapping("/pageWaitEntryHelp")
    public AjaxPageResult<BtHelpInputPageVO> pageWaitEntryHelp(@RequestBody BtHelpInputPageDTO btHelpInputPageDTO) {
        Page<BtHelp> page = PageHelper.startPage(btHelpInputPageDTO.getCurrPage(), btHelpInputPageDTO.getPageSize());
        BtHelp btHelp = BtHelpInputMapstruct.BT_HELP_INPUT_MAP_STRUCT.pagetableDto(btHelpInputPageDTO);
        List<BtHelp> btHelps = btHelpInputService.pageHelpInput(btHelp);
        List<BtHelpInputPageVO> btHelpInputPageVOS = BtHelpInputMapstruct.BT_HELP_INPUT_MAP_STRUCT.pagePustrespone(btHelps);
        return buildPageResult(btHelpInputPageVOS,page);

    }

    /**
     * 结果录入详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "结果录入详情", notes = "")
    @ApiImplicitParam(name = "fetchWaitReplyHelp", value = "结果录入详情", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:page')")
    @GetMapping("/fetchWaitEntryHelp")
    public AjaxResult<BtHelpInputDetialsVO> fetchWaitEntryHelp(@NotBlank(message = "ID不能为空") String id){
        BtHelp btHelp = btHelpInputService.detailsHelpInput(id);
        BtHelpInputDetialsVO btHelpInputDetialsVO = BtHelpInputMapstruct.BT_HELP_INPUT_MAP_STRUCT.bthelp_CHANGE_inputdetialsvo(btHelp);
        return AjaxResult.success("成功",btHelpInputDetialsVO);

    }


    /**
     *结果录入
     *
     * @param btHelpInputDTO
     * @return
     */
    @ApiOperation(value = "结果录入", notes = "")
    @ApiImplicitParam(name = "updateEntryHelp", value = "结果录入", required = true, dataType = "btHelpInputDTO")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:page')")
    @PostMapping("/updateEntryHelp")
    public AjaxResult<String> updateEntryHelp(@RequestBody BtHelpInputDTO btHelpInputDTO){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datestring = simpleDateFormat.format(date);
        BtHelp btHelp = BtHelpInputMapstruct.BT_HELP_INPUT_MAP_STRUCT.btHelpInputDTO_CHANGE_BtHelp(btHelpInputDTO);
        BtHelp verify = btHelpInputService.detailsHelpInput(btHelp.getId());
        int result = 0;
        if(verify.getStatus() == BtHelpStatusEnum.TO_BE_ENTERED){
            btHelp.setStatus(BtHelpStatusEnum.COMPLETED);
            btHelp.setEnterTime(datestring);
            result = btHelpInputService.updateInput(btHelp);
        }
        return toAjax(result,"审核成功","审核失败");

    }



    // * ================================================基础增删改查BEGIN============================================= *

}
